var user ={
    id:"",
    img:"",
    userName:''
}
var friend = {
    id:"",
    img:"",
    userName:''
}
var main_f ='' , main_c ='' , main = "消息"
let img = [{
    url:'/web/love-color.png',
    class:'myMouse-blue'
},{
    url:'/web/love-red.png',
    class:'myMouse-color'
},{
    url:'/web/love-blue.png',
    class:'myMouse-red'
},{
    url:'/web/love-pink.png',
    class:'myMouse-yellow'
},{
    url:'/web/love-yellow.png',
    class:'myMouse-pink'
}]

var files = [{
    name:'秃头的早期治疗和防范',
    url:'/web/context.txt',
    id:'66666668',
    type:'txt',
    time:'2021-7-20 20:30:24',
    big:'32.4KB',
    isLoad:0,
    from:'ltt'
},{
    name:'秃头的早期治疗和防范',
    url:'/web/context.txt',
    id:'66666668',
    type:'txt',
    time:'2021-7-20 20:30:24',
    big:'32.4KB',
    isLoad:1,
    from:'课设讨论组的群文件'
},{
    name:'秃头的早期治疗和防范',
    url:'/web/context.txt',
    id:'66666668',
    type:'txt',
    time:'2021-7-20 20:30:24',
    big:'32.4KB',
    isLoad:0,
    from:'dyj'
},{
    name:'秃头的早期治疗和防范',
    url:'/web/context.txt',
    id:'66666668',
    type:'txt',
    time:'2021-7-20 20:30:24',
    big:'32.4KB',
    isLoad:1,
    from:'老宇韬'
}]
/**
 * 鼠标
 */
function  kk(){
    setInterval(function (e) {
        const b = Math.floor(Math.random() * 4 + 0);
        document.getElementById("body").className = img[b].class

    },450)
}
let i = 0;
let s;

/**
 * onload加载页面内容
 */
function yong(){
    document.getElementById("消息").style.background = "#CED4DA"
    console.log(user)
    setInterval(function (e) {
        this.NewContent()
    },3000)
    this.createPeople();

};

//鼠标悬停
var x
var y ;
document.addEventListener('click',function (e) {
    
    //click 只要鼠标一点击，就会触发事件
    //获取鼠标最新的坐标
    y = e.pageY;
    x = e.pageX;
    setTimeout(xuanfu(),200)
    
})

function xuanfu(){
    document.getElementById("love_image").style.opacity = 1
     var a = Math.floor(Math.random()*4+0)    //取x-y之间的整数
    // document.getElementById("container").style.cursor = url(img[a].url)
    document.getElementById("love_image").src = img[a].url
     // rgba(0,0,0,0.5)
    // document.getElementById("love_image").css{()}
    let love = document.getElementById('love');
    console.log(x)
    console.log((y))
    love.style.top = (y-50).toString()+'px';
    love.style.left = (x-10).toString()+'px';
    var time = setInterval(function(e){
        if(document.getElementById("love_image").style.opacity > 0){
            document.getElementById("love_image").style.opacity = document.getElementById("love_image").style.opacity - 0.1
        }
        else{
            clearInterval(time)
        }
    },100)
}
var item = [{
    id:66666666,
    name:'爱你',
    img:'/web/img/man_avatar4.jpg',
    isChat:1,
    isRead:1,
    lastContent:'嘿你好',
    lastTime:'03:41',
    qian:'写作...'
},{
    id:66666667,
    name:'想你',
    img:'/web/img/man_avatar5.jpg',
    isChat:0,
    isRead:1,
    lastContent:'嘿你好',
    lastTime : '昨天',
    qian:'写作...'
},{
    id:666666668,
    name:'爱你',
    img:'/web/img/man_avatar4.jpg',
    isChat:0,
    isRead:1,
    lastContent:'嘿你好',
    lastTime:'上午11:08',
    qian:'写作...'
},{
    id:66666666,
    name:'想你',
    img:'/web/img/man_avatar5.jpg',
    isChat:0,
    isRead:0,
    lastContent:'嘿你好,我是李天泰，请问你叫什么名字',
    lastTime:'上午11:08',
    qian:'写作...'
},{
    id:66666667,
    name:'爱你',
    img:'/web/img/man_avatar4.jpg',
    isChat:0,
    isRead:0,
    lastContent:'嘿你好',
    lastTime:'上午11:08',
    qian:'写作...'
},{
    id:666666668,
    name:'想你',
    img:'/web/img/man_avatar5.jpg',
    isChat:0,
    isRead:0,
    lastContent:'嘿你好',
    lastTime:'上午11:08',
    qian:'写作...'
}]
var mast ;  //记录上次聊天的好友id

/**
 * onload加载页面内容（好友列表）
 */
function createPeople(e){

    $.ajax({
        type : "post",
        async : false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
        url : "chatRefresh",    //请求发送到UserServlet处
        data : {"Id" :user.id},
        success : function(result) {
            //请求成功时执行该函数内容，result即为服务器返回的json对象
            if((typeof result=='object')&&result.constructor==Object){
                obj = result;
            }else{
                obj  = eval("("+result+")");
            }
            console.log(obj)
            var itemss = []
            for(var i = 0; i < obj[0].friends.length ; i++) {
                var items = {
                    id: obj[0].friends[i].id,
                    userName: obj[0].friends[i].userName,
                    img:  obj[0].friends[i].img,
                    isChat: 0,
                    isRead: 0,
                    lastContent: '',
                    lastTime: '',
                    qian:  obj[0].friends[i].signature,
                    email:  obj[0].friends[i].email,
                    content:[]
                }
                var last= 0, isR = 0
                for(var j = 0 ; j < obj[0].message.length ; j++){
                    if(obj[0].message[j].fromId == obj[0].friends[i].id || obj[0].message[j].toId == obj[0].friends[i].id){
                        var content = {
                            content : obj[0].message[j].content,
                            ismy:0,
                            isRead:obj[0].message[j].isRead,
                            time:time(obj[0].message[j].sendTime),
                            type:0,
                            fromId:obj[0].message[j].fromId,
                            messageID:obj[0].message[j].messageId
                        }
                        if(obj[0].message[j].fromId == user.id){
                            content.ismy = 1
                        }
                        if(content.isRead == 0) isR++
                        items.content.push(content)
                        console.log(content)
                        last++
                    }
                }
                console.log(items)
                // console.log(last)
                items.lastTime = items.content[last-1].time
                items.lastContent = items.content[last-1].content
                items.isRead = isR
                if(items.qian == "") items.qian = "我的梦想是星辰和大海~~"
                itemss.push(items)
            }
            console.log(itemss)
            item = itemss
        },
        error : function(errorMsg) {
            //请求失败时执行该函数
            alert("请求数据失败!");
            console.log(errorMsg)
        }
    });
    var html = ''
    console.log(item)

    for( i = 1 ; i < item.length ; i++){
        var kk = item[0]
        if(item[i].isRead > item[0].isRead ) {
            item[0] = item[i]
            item[i] = kk
        }
    }
    item[0].isChat = 1
    friend = item[0]
    mast = item[0].id
    console.log(friend )
    console.log(mast)
    chat_content(friend)
    // var content = '你好'
    // document.getElementById("p2").style.width = content.length*25 +'px';
   chat_people(item)
}

/**
 * onload编写页面内容（好友列表）
 */
function  chat_people(item){
    var html = ''
    console.log(item)
    for(var i = 0 ; i < item.length ; i++){
        html+='<div class=\" line \"></div>'
        if(item[i].isChat == 1){
            html+='<div name=\"fri\" onclick=\" FriendsTap(\''+ item[i].id +'\') \" class = \"' + 'chat_lie1\" id=\"'+ item[i].id + '\">'
            document.getElementById("chat_img").src= item[i].img
            document.getElementById("chat_name").innerHTML= item[i].userName
            document.getElementById("chat_qian").innerHTML= item[i].qian
            mast = item[i].id
        }else{
            html+='<div name=\"fri\" onclick=\" FriendsTap(\''+ item[i].id +'\') \" class = \"' + 'chat_lie2\" id=\"'+ item[i].id + '\">'
        }
        html += '<img name=\"avatar_imgi\" src = \" '+ item[i].img + '\"' + 'class=\"' + 'chat_lie_img1 \"'+'>'
        html += '<div class = \" dot \"> </div>'
        html += '<div class=\"contentxt_container \">'
        if(item[i].isRead != 0){
            html += '<p class=\" name2 \">'+item[i].userName+"</p>"
        }else{
            html += '<p class=\" name1 \">'+item[i].userName+"</p>"
        }
        html += '<div class = \" contentxt \">' + item[i].lastContent +"</div>"
        html += '</div>'
        if(item[i].isRead == 0 || i == 0){
            html += '<p class = \" time2\">' + item[i].lastTime.split(' ')[1] + '</p>'

        }else{
            html += '<div class = \" time_container \">'
            html += ' <div class = \" center lasttime_container\" style = \" color:#fff ;font-size:10px;\">' + item[i].isRead + '</div>'
            html += '<p class = \"center time1\">' + item[i].lastTime.split(' ')[0] + '</p>'
            html += '</div>'
        }
        html+='</div>'

    }
    html+='<div class=\" line \"></div>'
    main_f = html
    document.getElementById("liebiao").innerHTML=html;
}

/**
 * onload编写页面内容（好友列表 中 最好聊天时间的属性）
 */
function time(longTypeDate){
    var time = longTypeDate.toString().split('')
    var year = '',mouth= '',day= '',hour = '',min = '',sec = ''
    for(var i = 0 ; i<14 ; i++){
        if(i<4) year+=time[i]
        else if(i<6) mouth+=time[i]
        else if(i<8) day+=time[i]
        else if(i<10) hour+=time[i]
        else if(i<12) min+=time[i]
        else sec+=time[i]
    }

    var dateType = year +'-' +mouth + '-' + day +  ' ' + hour + ':' + min  ;//yyyy-MM-dd格式日期
    return dateType;

}

/**
 * 好友列表点击切换事件
 */
function FriendsTap(data){
    if(mast != data){
        console.log(data)
        document.getElementById(data).className = 'chat_lie1'
        document.getElementById(mast).className = "chat_lie2"
        mast = data
        for(i = 0 ; i <item.length ;i++){
            item[i].isChat = 0
            if(item[i].id == mast){
                friend = item[i]
            }
        }
        friend.isRead = 1
        friend.isRead = 0
        chat_content(friend)
        document.getElementById("chat_img").src= friend.img
        document.getElementById("chat_name").innerHTML= friend.userName
        document.getElementById("chat_qian").innerHTML= friend.qian
    }

}

/**
 * onload编写页面内容（好友聊天框内容）
 */
function chat_content(item){
    console.log(item)
    console.log(item.content.length)
    console.log(item.content)
    var html = ''
    for(var i = 0 ; i < item.content.length ; i++){
        var kk = item.content[i].time.split(" ")
        var kkk = kk[1].split(":")[0]
        if(item.content[i].ismy == 0){
            if(i == 0) html += "<div class=\"mychat\" style=\"margin-top: 20px\">"  + '<div class=\"flex_r chat_info1\">'
            else html += "<div class=\"mychat\">"  + '<div class=\"flex_r chat_info1\">'
            html += ' <img class=\"avaterimg\" src=\"'+user.img+'\" alt=\"\">'
            html += '<div class=\"flex_x\">'
            html += '<p class=\"name1\" style=\"margin-left: 10px;\">'+user.userName+'</p>'
            html += ' <div class=\"flex_r info\">\n'
            if(kkk < 12) html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '上午' + '</i>' + kk[1] +'</p>'
            else html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '下午' + '</i>' + kk[1] +'</p>'
            html += ' <img src=\"./web/image/finish.png\" style=\"height:22px;width:22px\" alt=\"\">'
            html += '</div>'
            html += '</div>'
            html += '</div>'
            html += ' <div id=\"'+'+\" class=\"mychat_kuang center\">'+item.content[i].content+'</div>'
            html += '</div>'
            html += '</div>'

        }else{
            if(i == 0) html += "<div class=\"hischat\" style='margin-top: 20px'>"  + '<div class=\"flex_r chat_info\">'
            else html += "<div class=\"hischat\" >"  + '<div class=\"flex_r chat_info\">'
            html += ' <img class=\"avaterimg\" src=\"'+friend.img+'\" alt=\"\">'
            html += '<div class=\"flex_x\">'
            html += '<p class=\"name1\" style=\"margin-left: 10px;\">'+friend.userName+'</p>'
            html += ' <div class=\"flex_r info\">\n'
            if(kkk < 12) html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '上午' + '</i>' + kk[1] +'</p>'
            else html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '下午' + '</i>' + kk[1] +'</p>'
            html += ' <img src=\"./web/image/finish.png\" style=\"height:22px;width:22px\" alt=\"\">'
            html += '</div>'
            html += '</div>'
            html += '</div>'
            html += ' <div id=\"'+item.content[i].messageID+'\" class=\"hischat_kuang center\">'+item.content[i].content+'</div>'
            html += '</div>'
            html += '</div>'
        }

    }
    main_c = html
    document.getElementById("chat").innerHTML = html
}

/**
 * 聊天输入框处理事件
 */
function chat(){
    if(document.getElementById("title_main") === "聊天"){
        console.log(222)
        var content = $("#content").val();
        console.log(content)
        $.ajax({
            type : "post",
            async : true,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
            url : "chat_Content",    //请求发送到UserServlet处
            data : {"content": content,"id": user.id, "toId" :mast, "type":"0"},
            success : function(result) {
                //请求成功时执行该函数内容，result即为服务器返回的json对象
                // for(var i=0;i<result.length;i++){
                //     alert(result[i].id+result[i].uname+result[i].nickname+result[i].pwd);
                // }

                let date = new Date();
                let kkk = date.getHours()
                result =eval('('+result+')');
                var html = main_c
                var kk = date.getMinutes()
                console.log(result)
                html += "<div class=\"mychat\" style=\"margin-top: 20px\">"  + '<div class=\"flex_r chat_info1\">'
                html += ' <img class=\"avaterimg\" src=\"'+user.img+'\" alt=\"\">'
                html += '<div class=\"flex_x\">'
                html += '<p class=\"name1\" style=\"margin-left: 10px;\">'+user.userName+'</p>'
                html += ' <div class=\"flex_r info\">\n'
                if(kkk < 12) html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '上午' + '</i>' + kkk+':'+kk  +'</p>'
                else html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '下午' + '</i>' + kkk+':'+kk +'</p>'
                html += ' <img src=\"./web/image/finish.png\" style=\"height:22px;width:22px\" alt=\"\">'
                html += '</div>'
                html += '</div>'
                html += '</div>'
                html += ' <div id=\"'+'+\" class=\"mychat_kuang center\">'+content+'</div>'
                html += '</div>'
                html += '</div>'
                // console.log(item[i])
                // friend.content = result
                // chat_content(item[i])
                //隐藏加载动画略
                document.getElementById("chat").innerHTML = html
            },
            error : function(errorMsg) {
                //请求失败时执行该函数
                alert("请求数据失败!");
                console.log(errorMsg)
            }
        });
        document.getElementsByName("content").value = ""
    }else if (document.getElementById("title_main") === "群聊"){
        console.log(33)
        var c
        for(i = 0 ; i < quns.length ; i++) {
            if(quns[i].isChat == 1){
                c = i
            }
        }
        var content = $("#content").val();
        console.log(content)
        $.ajax({
            type : "post",
            async : true,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
            url : "chat_Content",    //请求发送到UserServlet处
            data : {"content": content,"id": user.id, "toId" : "1" , "type" : quns[c].id},
            success : function(result) {
                //请求成功时执行该函数内容，result即为服务器返回的json对象
                // for(var i=0;i<result.length;i++){
                //     alert(result[i].id+result[i].uname+result[i].nickname+result[i].pwd);
                // }

                let date = new Date();
                let kkk = date.getHours()
                result =eval('('+result+')');
                var html = main_c
                var kk = date.getMinutes()
                console.log(result)
                html += "<div class=\"mychat\" style=\"margin-top: 20px\">"  + '<div class=\"flex_r chat_info1\">'
                html += ' <img class=\"avaterimg\" src=\"'+user.img+'\" alt=\"\">'
                html += '<div class=\"flex_x\">'
                html += '<p class=\"name1\" style=\"margin-left: 10px;\">'+user.userName+'</p>'
                html += ' <div class=\"flex_r info\">\n'
                if(kkk < 12) html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '上午' + '</i>' + kkk+':'+kk  +'</p>'
                else html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '下午' + '</i>' + kkk+':'+kk +'</p>'
                html += ' <img src=\"./web/image/finish.png\" style=\"height:22px;width:22px\" alt=\"\">'
                html += '</div>'
                html += '</div>'
                html += '</div>'
                html += ' <div id=\"'+'+\" class=\"mychat_kuang center\">'+content+'</div>'
                html += '</div>'
                html += '</div>'
                // console.log(item[i])
                // friend.content = result
                // chat_content(item[i])
                //隐藏加载动画略
                document.getElementById("chat").innerHTML = html
            },
            error : function(errorMsg) {
                //请求失败时执行该函数
                alert("请求数据失败!");
                console.log(errorMsg)
            }
        });
        document.getElementsByName("content").value = ""
    }

}

/**
 * 获取同步聊天消息
 */
function NewContent(){
    $.ajax({
        type : "post",
        async : false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
        url : "RequestNew",    //请求发送到UserServlet处
        data : {"id" :user.id},
        success : function(result) {
            if((typeof result=='object')&&result.constructor==Object){
                obj = result;
            }else{
                obj  = eval('('+result+')');
            }
            console.log(obj)
            // console.log(result.valueOf("NO"));
        },
        error : function(errorMsg) {
            //请求失败时执行该函数
            // console.log(errorMsg)
        }
    });
}

/**
 * 消息，群聊等标签切换事件
 */
var lastIII = "消息"
function tap(id){
    if(lastIII != id ){
        console.log(id)
        console.log(lastIII)
        document.getElementById(id).style.background = "#CED4DA"
        document.getElementById(lastIII).style.background = "#F2F2F2"
    }
    lastIII = id
    if(id == "消息"){
        yong()
        document.getElementById("title_main").innerHTML= "聊天"
        document.getElementById("search").placeholder = "搜索聊天"
    }else if(id == "群聊"){
        document.getElementById("title_main").innerHTML= "群聊"
        qun()
    }else if(id == "好友"){
        document.getElementById("title_main").innerHTML= "好友组"
        chat_people([])
        $.ajax({
            type : "post",
            async : false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
            url : "SearchPeople",    //请求发送到UserServlet处
            data : {"id" :user.id,"type" : '0'},
            success : function(result) {
                result =eval('('+result+')');
                console.log(result)
                var kkk = [] , c = 0
                for(i = 0 ; i< result.length;i++){
                    kkk.push(result[i])
                    c++
                }

                if(c!=0) {
                    list_people(kkk)
                }else{
                    var html = ''
                    html += "<div style='color:gray;text-align: center;align-self: center'>未搜索到任何结果</div>"
                    document.getElementById("liebiao").innerHTML = html
                }
                // console.log(result.valueOf("NO"));
            },
            error : function(errorMsg) {
                //请求失败时执行该函数
                // console.log(errorMsg)
                var html = ''
                html += "<div style='color:gray;text-align: center;align-self: center'>未搜索到任何结果</div>"
                document.getElementById("liebiao").innerHTML = html
            }
        });

        list_people(item)

        document.getElementById("search").placeholder = "搜索好友"
    }else{         //消息通知
        file()
        document.getElementById("title_main").innerHTML= "消息通知"
    }
}

/**
 * 编写页面内容（群聊列表）
 */
var quns = []
function qun(){
    document.getElementById("chat").innerHTML=""
    document.getElementById("chat_name").innerHTML=""
    document.getElementById("chat_qian").innerHTML=""
    document.getElementById("chat_img").src= ""
    document.getElementById("liebiao").innerHTML = ""
    $.ajax({
        type : "post",
        async : false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
        url : "chatQun",    //请求发送到UserServlet处
        data : {"Id" :user.id},
        success : function(result) {
            //请求成功时执行该函数内容，result即为服务器返回的json对象
            if((typeof result=='object')&&result.constructor==Object){
                obj = result;
            }else{
                obj  = eval("("+result+")");
            }

            console.log(obj)
            var itemsss = []
            for(i = 0 ; i < obj.length ; i++){
                if(obj[i].chatGroup.delFlag !== "1"){
                    var itemss = {
                        id: obj[i].chatGroup.chatGroupId,  //id
                        img: obj[i].chatGroup.img,    //群头像
                        name: obj[i].chatGroup.name, //名字
                        master: obj[i].chatGroup.master,  //群主id
                        remarks: obj[i].chatGroup.remarks,  //标记
                        message: [],
                        isChat:0,
                        users:[]
                    }
                    for(var j = 0;j<obj[i].message.length;j++){
                        var message = {
                            content:obj[i].message[j].content,
                            fromId:obj[i].message[j].fromId,
                            user:{},
                            messageID:obj[i].message[j].messageId,
                            time:time(obj[i].message[j].sendTime),
                        }

                        for(var k = 0 ; k< obj[i].users.length;k++){
                            if(message.fromId == obj[i].users[k].id){
                                var items = {
                                    email: obj[i].users[k].email,
                                    id:obj[i].users[k].id,
                                    img:obj[i].users[k].img,
                                    mobile:obj[i].users[k].mobile,
                                    signature: "我的梦想是星辰和大海~~",
                                    userName:obj[i].users[k].userName
                                }

                                message.user = items
                            }
                            var itemssss = {
                                email: obj[i].users[k].email,
                                id:obj[i].users[k].id,
                                img:obj[i].users[k].img,
                                mobile:obj[i].users[k].mobile,
                                signature: "我的梦想是星辰和大海~~",
                                userName:obj[i].users[k].userName
                            }
                            itemss.users.push(itemssss)
                        }
                        itemss.message.push(message)
                    }
                }
                itemsss.push(itemss)
            }
            console.log(itemsss)
            quns = itemsss
            quns[0].isChat = 1
            creat_list_qun(quns)
            create_content(quns[0])
        },
        error : function(errorMsg) {
            //请求失败时执行该函数
            alert("请求数据失败!");
            console.log(errorMsg)
        }
    });
}

/**
 * 编写页面内容（群聊列表）
 */
var main_q
function creat_list_qun(item){
    var html = ''
    for(i = 0 ; i <item.length ; i++){
        html+='<div class=\" line \"></div>'
        if(item[i].isChat == 1){
            html+='<div onclick=\" QUNtap(\''+ item[i].id +'\') \" class = \"' + 'chat_lie1\" id=\"'+ item[i].id + '\">'
            document.getElementById("chat_img").src= item[i].img
            document.getElementById("chat_name").innerHTML= item[i].name
            document.getElementById("chat_qian").innerHTML= item[i].remarks
            mast = item[i].id
        }else{
            html+='<div onclick=\" QUNtap(\''+ item[i].id +'\') \" class = \"' + 'chat_lie2\" id=\"'+ item[i].id + '\">'
        }
        html += '<img src = \" '+ item[i].img + '\"' + 'class=\"' + 'chat_lie_img1 \"'+'>'
        html += '<div class = \" dot \"> </div>'
        html += '<div class=\"contentxt_container \">'
        html += '<p class=\" name2 \">'+item[i].name+"</p>"

        html += '<div class = \" contentxt \">' + item[i].remarks +"</div>"
        html += '</div>'

        html+='</div>'

    }
    html+='<div class=\" line \"></div>'
    main_q = html
    document.getElementById("liebiao").innerHTML = html;

}

/**
 * 编写页面内容（群聊聊天框内容）
 */
function create_content(item){
    var html = ''
    for(var i = 0 ; i < item.message.length ; i++){
        var kk = item.message[i].time.split(" ")
        var kkk = kk[1].split(":")[0]
        if(item.message[i].fromId == user.id){
            if(i == 0) html += "<div class=\"mychat\" style=\"margin-top: 20px\">"  + '<div class=\"flex_r chat_info1\">'
            else html += "<div class=\"mychat\">"  + '<div class=\"flex_r chat_info1\">'
            html += ' <img class=\"avaterimg\" src=\"'+user.img+'\" alt=\"\">'
            html += '<div class=\"flex_x\">'
            html += '<p class=\"name1\" style=\"margin-left: 10px;\">'+user.userName+'</p>'
            html += ' <div class=\"flex_r info\">\n'
            if(kkk < 12) html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '上午' + '</i>' + kk[1] +'</p>'
            else html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '下午' + '</i>' + kk[1] +'</p>'
            html += ' <img src=\"./web/image/finish.png\" style=\"height:22px;width:22px\" alt=\"\">'
            html += '</div>'
            html += '</div>'
            html += '</div>'
            html += ' <div id=\"'+'+\" class=\"mychat_kuang center\">'+item.message[i].content+'</div>'
            html += '</div>'
            html += '</div>'

        }else{
            if(i == 0) html += "<div class=\"hischat\" style='margin-top: 20px'>"  + '<div class=\"flex_r chat_info\">'
            else html += "<div class=\"hischat\" >"  + '<div class=\"flex_r chat_info\">'
            html += ' <img class=\"avaterimg\" src=\"'+item.message[i].user.img+'\" alt=\"\">'
            html += '<div class=\"flex_x\">'
            html += '<p class=\"name1\" style=\"margin-left: 10px;\">'+item.message[i].user.userName+'</p>'
            html += ' <div class=\"flex_r info\">\n'
            if(kkk < 12) html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '上午' + '</i>' + kk[1] +'</p>'
            else html += '  <p class=\"time2\"> <i class=\"time2\" style=\"margin-right: 5px;\"> ' + '下午' + '</i>' + kk[1] +'</p>'
            html += ' <img src=\"./web/image/finish.png\" style=\"height:22px;width:22px\" alt=\"\">'
            html += '</div>'
            html += '</div>'
            html += '</div>'
            html += ' <div id=\"'+item.message[i].messageID+'\" class=\"hischat_kuang center\">'+item.message[i].content+'</div>'
            html += '</div>'
            html += '</div>'
        }

    }
    main_c = html
    document.getElementById("chat").innerHTML = html
}

/**
 * 查询处理事件
 */
function search(){
    if(document.getElementById("search").placeholder === "搜索聊天"){
        console.log(item)
        var kkk = [] ,c = 0
        var searchID = $("#search").val();
        console.log("1111"+searchID)
        for( i = 0 ; i < item.length ; i++){
            item[i].isChat = 0
            if(item[i].id == searchID){
                kkk.push(item[i])
                c++
            }else if(item[i].userName.indexOf(searchID) != -1){
                kkk.push(item[i])
                c++
            }
        }
        if(c!=0) {
            kkk[0].isChat = 1
            chat_people(kkk)
            chat_content(kkk[0])
        }else{
            chat_people(kkk)
            document.getElementById("chat").innerHTML=""
            document.getElementById("chat_name").innerHTML=""
            document.getElementById("chat_qian").innerHTML=""
            document.getElementById("chat_img").src= ""
            html += "<div style='color:gray;text-align: center;align-self: center'>未搜索到任何结果</div>"
            document.getElementById("liebiao").innerHTML = html
        }
    }else if(document.getElementById("search").placeholder === "搜索好友"){
        var kkk = [] ,c = 0
        var searchID = $("#search").val();
        console.log("1111"+searchID)
        $.ajax({
            type : "post",
            async : false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
            url : "SearchPeople",    //请求发送到UserServlet处
            data : {"id" :searchID,"type": '1'},
            success : function(result) {
                result =eval('('+result+')');
                console.log(result)
                kkk.push(result[0])
                c++
                if(c!=0) {

                    list_people(kkk)
                }else{
                    var html = ''
                    html += "<div style='color:gray;text-align: center;align-self: center'>未搜索到任何结果</div>"
                    document.getElementById("liebiao").innerHTML = html
                }
                // console.log(result.valueOf("NO"));
            },
            error : function(errorMsg) {
                //请求失败时执行该函数
                // console.log(errorMsg)
                var html = ''
                html += "<div style='color:gray;text-align: center;align-self: center'>未搜索到任何结果</div>"
                document.getElementById("liebiao").innerHTML = html
            }
        });

    }else if(document.getElementById("search").placeholder === "搜索群聊") {
        var kkk = [], c = 0
        var searchID = $("#search").val();
        console.log("1111" + searchID)
        for (i = 0; i < quns.length; i++) {
            quns[i].isChat = 0
            if (quns[i].id.indexOf(searchID) !== -1) {
                kkk.push(quns[i])
                c++
            } else if (quns[i].name.indexOf(searchID) !== -1) {
                kkk.push(quns[i])
                c++
            }
        }
        if (c != 0) {
            kkk[0].isChat = 1
            creat_list_qun(kkk)
            create_content(kkk[0])
        } else {
            creat_list_qun(kkk)
            document.getElementById("chat").innerHTML = ""
            document.getElementById("chat_name").innerHTML = ""
            document.getElementById("chat_qian").innerHTML = ""
            document.getElementById("chat_img").src = ""
            html += "<div style='color:gray;text-align: center;align-self: center'>未搜索到任何结果</div>"
            document.getElementById("liebiao").innerHTML = html
        }
    }
}

/**
 * 监听查询事件（注销）
 */
// $("#search").change(function(event){
//     //执行相关操作
//
//     if( $("#search").val() === "") {
//         chat_people(item)
//         chat_content(item[0])
//     }
// })

/**
 * 编写页面内容（好友列表）
 */
function list_people(item){
    var html = ''
    console.log(item)
    for(var i = 0 ; i < item.length ; i++){
        html+='<div class=\" line \"></div>'
        if(item[i].isChat === 1){
            html+='<div  onclick=\" FriendsTap(\''+ item[i].id +'\') \" class = \"' + 'chat_lie1\" id=\"'+ item[i].id + '\">'
            document.getElementById("chat_img").src= item[i].img
            document.getElementById("chat_name").innerHTML= item[i].userName
            if(item[i].signature === "" )  {
                document.getElementById("chat_qian").innerHTML= "我们的旅途是星辰和大海~"
            }
            mast = item[i].id
        }else{
            html+='<div name=\"fri\" onclick=\" FriendsTap(\''+ item[i].id +'\') \" class = \"' + 'chat_lie2\" id=\"'+ item[i].id + '\">'
        }
        html += '<img name=\"avatar_imgi\" src = \" '+ item[i].img + '\"' + 'class=\"' + 'chat_lie_img1 \"'+'>'
        html += '<div class = \" dot \"> </div>'
        html += '<div class=\"contentxt_container \">'
        if(item[i].isRead != 0){
            html += '<p class=\" name2 \">'+item[i].userName+"</p>"
        }else{
            html += '<p class=\" name1 \">'+item[i].userName+"</p>"
        }
        html += '<div class = \" contentxt \">' + item[i].qian +"</div>"
        html += '</div>'

        html+='</div>'

    }
    html+='<div class=\" line \"></div>'
    main_f = html
    document.getElementById("liebiao").innerHTML=html;
}

/**
 * 显示加好友的输入框
 */
function people_content(){
    console.log((111))
    setTimeout(function (){
        document.getElementById("modal").style.opacity =  "1";
        document.getElementById("modal_").style.background = "rgba(255, 255, 255, 0.4)" ;
        },200)
}

/**
 * 关闭加好友的输入框
 */
function closes(){
    setTimeout(function (){
        document.getElementById("modal").style.opacity =  "0";
        document.getElementById("modal_").style.background = "rgba(255, 255, 255, 0)" ;
    },200)
}

/**
 * 加好友处理事件
 */
function add(){
    var id = $("#friendID").val();
    $.ajax({
        type : "post",
        async : false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
        url : "addPeople",    //请求发送到UserServlet处
        data : {"id" :id,"myID":user.id},
        success : function(result) {
            result =eval('('+result+')');
            console.log(result)
            setTimeout(function (){
               closes()
            },200)
            confirm("添加好友成功!");
            tap("好友")
        },
        error : function(errorMsg) {
            //请求失败时执行该函数
            // console.log(errorMsg)
            confirm("添加好友失败!");
        }
    });
}

/**
 * 群聊的切换处理事件
 */
var qunss = quns[0].id
var qunnnn
function QUNtap(data){
    if(qunss != data){
        console.log(data)
        document.getElementById(data).className = 'chat_lie1'
        document.getElementById(qunss).className = "chat_lie2"
        mast = data
        for(i = 0 ; i <item.length ;i++){
            if(item[i].id == qunss){
                qunnnn = quns[i]
            }
        }
        create_content(qunnnn)
        document.getElementById("chat_img").src= qunnnn.img
        document.getElementById("chat_name").innerHTML= qunnnn.name
        document.getElementById("chat_qian").innerHTML= qunnnn.signature
    }
}

/**
 * 编写页面内容（切换背景）
 */
function change_bg(){
    console.log(22222)
    console.log(document.getElementById("container_").style.background)
    document.getElementById("container_").style.background = "rgb(26,34,54)"
    if(document.getElementById("container_").style.background === "rgb(242, 242, 242)"){
        document.getElementById("container_").style.background = "rgb(26,34,54)"
    }else{
        document.getElementById("container_").style.background = "rgb(242, 242, 242)"
    }
}

/**
 * 显示加群聊输入框
 */
function addQU(){
    document.getElementById("modal___").style.opacity =  "1";
    document.getElementById("modal__").style.background = "rgba(255, 255, 255, 0.4)" ;
}

/**
 * 加群聊的处理事件
 */
function addQQ(){
    var id = $("#QID").val();
    $.ajax({
        type : "post",
        async : false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
        url : "addQQ",    //请求发送到UserServlet处
        data : {"id" :id,"myID":user.id},
        success : function(result) {
            if((typeof result=='object')&&result.constructor==Object){
                obj = result;
            }else{
                obj  = eval("("+result+")");
            }
            console.log(obj)
            var itemsss = []
            for(i = 0 ; i < obj.length ; i++){
                if(obj[i].chatGroup.delFlag !== "1"){
                    var itemss = {
                        id: obj[i].chatGroup.chatGroupId,  //id
                        img: obj[i].chatGroup.img,    //群头像
                        name: obj[i].chatGroup.name, //名字
                        master: obj[i].chatGroup.master,  //群主id
                        remarks: obj[i].chatGroup.remarks,  //标记
                        message: [],
                        isChat:0,
                        users:[]
                    }
                    for(var j = 0;j<obj[i].message.length;j++){
                        var message = {
                            content:obj[i].message[j].content,
                            fromId:obj[i].message[j].fromId,
                            user:{},
                            messageID:obj[i].message[j].messageId,
                            time:time(obj[i].message[j].sendTime),
                        }

                        for(var k = 0 ; k< obj[i].users.length;k++){
                            if(message.fromId == obj[i].users[k].id){
                                var items = {
                                    email: obj[i].users[k].email,
                                    id:obj[i].users[k].id,
                                    img:obj[i].users[k].img,
                                    mobile:obj[i].users[k].mobile,
                                    signature: "我的梦想是星辰和大海~~",
                                    userName:obj[i].users[k].userName
                                }
                                message.user = items
                            }
                            var itemssss = {
                                email: obj[i].users[k].email,
                                id:obj[i].users[k].id,
                                img:obj[i].users[k].img,
                                mobile:obj[i].users[k].mobile,
                                signature: "我的梦想是星辰和大海~~",
                                userName:obj[i].users[k].userName
                            }
                            itemss.users.push(itemssss)
                        }
                        itemss.message.push(message)
                    }
                }
                itemsss.push(itemss)
            }
            quns.push(itemsss)
            creat_list_qun(quns)
        },
        error : function(errorMsg) {
            //请求失败时执行该函数
            // console.log(errorMsg)
            confirm("添加好友失败!");
        }
    });
}

/**
 * 关闭加群聊输入框
 */
function close1(){
    setTimeout(function (){
        document.getElementById("modal___").style.opacity =  "0";
        document.getElementById("modal__").style.background = "rgba(255, 255, 255, 0)" ;
    },200)
}

/**
 * 新文件处理事件
 */
function NEWfile(){

}

/**
 * 拉取所有文件消息
 */

function letfile(){
    $.ajax({
        type : "post",
        async : false,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
        url : "FileUploadServlet",    //请求发送到UserServlet处
        data : {"id" :id,"myID":user.id},
        success : function(result) {
            result =eval('('+result+')');
            console.log(result)
            setTimeout(function (){
                closes()
            },200)
            confirm("添加好友成功!");
            tap("好友")
        },
        error : function(errorMsg) {
            //请求失败时执行该函数
            // console.log(errorMsg)
            confirm("添加好友失败!");
        }
    });
}

/**
 * 编写页面内容（显示文件）
 */

function file(){
    var html = ''
    for(i= 0 ; i < files.length ; i++){
        html += "<div class='flie_container' style='margin-left: 30px;margin-top: 30px'>";
        html += "<div class='flex_r' style='border-bottom: 1px #FFFFFF solid;padding: 10px'>";
        html += "<img  src='/web/img/wenjian.png' >"
        html += "<div class='flex_c' style='height:80%;width:92%'> ";
        html += "<div class='title_'>"+ files[i].name +"."+files[i].type+   "<p style='color: #888888 ;font-size:17px;letter-spacing: 1px'>("+files[i].big+")</p>"+"</div>"
        if(files[i].isLoad == 1) html += "<div style='style='color:#B9B9B9;font-size:16px;letter-spacing:1px;align-self:left;margin-top:10px'>下载成功</div>"
        else html += "<div style='style='color:#888888;font-size:19px;letter-spacing:1px;align-self:left;margin-top:10px'>还未下载</div>"
        html += "</div>";

        html += "</div>";

        html += "<div class='flex_r'>"
        html += "<div class='k' style='font-size:12px;color:gray'>来自"+files[i].from+"</div>";
        html += "<div class='flex_r' style='width:50%;font-size:15px;color:#12B7F5'>";
        html += "<p>打开</p>"
        html += "<p>打开文件夹</p>"
        html += "<p>转发</p>"
        html += "</div>";
        html += "</div>";
        html += "</div>";
        document.getElementById("chat").innerHTML = html
    }
}